package com.cat.greedyAlgorithm;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/removing-minimum-number-of-magic-beans/
 * @create 2025/10/10 17:10
 * @since JDK17
 */

public class Solution73 {
    public long minimumRemoval(int[] beans) {
        int n = beans.length;
        long ans = Long.MAX_VALUE / 2, sum = 0, right = 0, left = 0;
        Arrays.sort(beans);
        for (int num : beans) {
            sum += num;
        }
        for (int i = n - 1; i >= 0; i--) {
            ans = Math.min(ans, sum - (long)beans[i] * (n - i));
            right += beans[i];
        }

        return ans;
    }
}
